package com.leancloud.circle.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.aries.ui.view.title.TitleBarView;
import com.leancloud.circle.R;
import com.leancloud.circle.adapter.MeCircleAdapter;
import com.leancloud.circle.bean.CircleBean;
import com.leancloud.circle.manage.CirclePathManage;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.Collections;
import java.util.List;

import tech.com.commoncore.base.BaseTitleActivity;
import tech.com.commoncore.view.SpaceItemDecoration;

@Route(path = CirclePathManage.CIRCLE_MY_ARTICLE)
public class MyArticleActivity extends BaseTitleActivity {

    private SmartRefreshLayout smartRefreshLayout;
    private RecyclerView recyclerView;
    private MeCircleAdapter circleAdapter;

    private List<CircleBean> circleBeans;   //圈子列表

    @Override
    public void setTitleBar(TitleBarView titleBar) {
        titleBar.setTitleMainText("我的文章");
    }

    @Override
    public int getContentLayout() {
        return R.layout.activity_my_article;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        smartRefreshLayout = findViewById(R.id.smart_refresh_layout);
        recyclerView = findViewById(R.id.my_article_recycler);

        circleAdapter = new MeCircleAdapter(R.layout.circle_layout_item_me_circle);
        int space = (int) getResources().getDimension(R.dimen.dp_5);
        SpaceItemDecoration decoration = new SpaceItemDecoration(space);
        recyclerView.addItemDecoration(decoration);
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerView.setAdapter(circleAdapter);

        smartRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                handlerRequestMySuccess();
            }
        });

        if (circleBeans == null || circleBeans.isEmpty()) {
            smartRefreshLayout.autoRefresh();
        }
    }

    private void handlerRequestMySuccess() {
//        CircleGlobal.getCircleImp().requestCircle(1, 20, null, -1, UserGlobal.getCurrentUser().userId, 0, null, new CircleBack<List<CircleBean>>() {
//            @Override
//            public void onBack(List<CircleBean> beans, String err) {
//                smartRefreshLayout.finishRefresh();
//                if (err == null && beans != null) {
//                    circleBeans = beans;
//                }
//                refreshCircleView(circleBeans);
//            }
//        });
    }

    private void refreshCircleView(List<CircleBean> circleBeans) {
        if (circleBeans != null && !circleBeans.isEmpty()) {
            Collections.reverse(circleBeans);
            circleAdapter.replaceData(circleBeans);
            circleAdapter.notifyDataSetChanged();
        }
    }
}
